<?php
namespace Home\Controller;

use Think\Controller;

class PayController extends Controller
{


    public function recharge()
    {
        if (IS_POST) {
           $total_fee = I('post.total_fee');
            //获取用户openid
            $user = D('User')->where(array('id'=>session('user_id')))->find();
            $openid = $user['openid'];
            $order_sn = date('YmdHis').session('user_id');
            $total  = $total_fee*100;
			$total = 1;
            $notify_url = 'http://'.$_SERVER['HTTP_HOST'].'/Home/Pay/notify';
            vendor("Wxpay.WxPayJsApiPay");
            $tools = new \JsApiPay();
            $input = new \WxPayUnifiedOrder();

            $input->SetBody("飞百客单车");
            $input->SetAttach($order_sn);
            $input->SetOut_trade_no($order_sn);
            $input->SetTotal_fee($total);
            $input->SetTime_start(date("YmdHis"));
            $input->SetTime_expire(date("YmdHis", time() + 600));
            $input->SetGoods_tag("飞百客单车");
            $input->SetNotify_url($notify_url);
            $input->SetTrade_type("JSAPI");
            $input->SetOpenid($openid);
            $order = \WxPayApi::unifiedOrder($input);
			
            $jsApiParameters = $tools->GetJsApiParameters($order);
            exit($jsApiParameters); 
           // $this->success(json_decode($jsApiParameters, true));
        }
    }
	public function pay_order()
    {
        if (IS_POST) {
			$order = session('order_info');
			$order || $this->error('订单已失效！');
            $total_fee = $order['order_price'];
            //获取用户openid
            $user = D('User')->where(array('id'=>session('user_id')))->find();
            $openid = $user['openid'];
            $total  = $total_fee*100;
			$total = 1;//暂时使用
            vendor("Wxpay.WxPayJsApiPay");
			
            $tools = new \JsApiPay();
            $input = new \WxPayUnifiedOrder();

            $input->SetBody("飞百客单车");
            $input->SetAttach($order['order_sn']);
            $input->SetOut_trade_no($order['order_sn']);
            $input->SetTotal_fee($total);
            $input->SetTime_start(date("YmdHis"));
            $input->SetTime_expire(date("YmdHis", time() + 600));
            $input->SetGoods_tag("飞百客单车");
            $input->SetNotify_url('http://'.$_SERVER['HTTP_HOST'].'/Home/Pay/order_notify');
            $input->SetTrade_type("JSAPI");
            $input->SetOpenid($openid);
            $order = \WxPayApi::unifiedOrder($input);
            $jsApiParameters = $tools->GetJsApiParameters($order);
            exit($jsApiParameters); 
           // $this->success(json_decode($jsApiParameters, true));
        }
    }
    public function notify()
    {
        //\Think\Log::write('测试日志信息，这是警告级别，并且实时写入');
        $xml      = isset($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS["HTTP_RAW_POST_DATA"] : "";
        $xmlObj   = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $xmlArr   = json_decode(json_encode($xmlObj), true);
        $order ='';
        if($xmlArr['return_code']=='SUCCESS'){
            $total_fee = $xmlArr['total_fee']/100;
            $order = $xmlArr['attach'];
            $user_id = substr($order,14);
            if($total_fee){
                D('User')->where(array('id'=>$user_id))->setInc('balance',$total_fee);
                $data['user_id'] = $user_id;
                $data['pay_type'] = '4';
                $data['pay_value'] = $total_fee;
                D('PayLog')->add($data);
                exit('SUCCESS');
            }else{
                exit('FAIL');
            }
        }
        exit('FAIL');
    }
	public function order_notify()
    {
        $xml      = isset($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS["HTTP_RAW_POST_DATA"] : "";
        $xmlObj   = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $xmlArr   = json_decode(json_encode($xmlObj), true);
        if($xmlArr['return_code']=='SUCCESS'){
            $total_fee = $xmlArr['total_fee']/100;
            $order_sn = $xmlArr['attach'];
            if($total_fee){
				$order_info = D('Order')->where(array('order_sn'=>$order_sn))->find();
				if($order_info['pay_status'] == '1'){
					$save_data = array();
					$save_data['pay_status'] = $order_info['service'] == '3'?'2':'3';//1.未支付 2.支付首款/押金 3.已支付
					$save_data['pay_cash'] = $total_fee;
					$save_data['pay_time'] = time();
					$save_data['status'] = 2;
					D('Order')->where(array('id'=>$order_info['id']))->save($save_data);
					//支付日志
					$log_data = array();
					$log_data['user_id'] = $order_info['user_id'];
					$log_data['order_id'] = $order_info['id'];
					$log_data['pay_type'] = '1';
					$log_data['pay_value'] = $total_fee;
					D('PayLog')->add($log_data);
					//分期付款记录
					if($order_info['service'] == '3'){
						$goods_info = D('Goods')->where(array('id'=>$order_info['goods_id']))->find();
						$repayment = array();
						$repayment['user_id'] = $order_info['user_id'];
						$repayment['order_id'] = $order_info['id'];
						$repayment['remain_cash'] = $order_info['goods_price']-$order_info['order_price'];
						$repayment['remain_days'] = $goods_info['per_days'];
						$repayment['per_days'] = $goods_info['per_days'];
						$repayment['per_cash'] = $goods_info['per_price'];
						D('Repayment')->add($repayment);
					}
					//销量增加
					D('Goods')->where(array('id'=>$order_info['goods_id']))->setInc('sale_count',$order_info['order_num']);
				}
                exit('SUCCESS');
            }else{
                exit('FAIL');
            }
        }
        exit('FAIL');
    }
}
